import java.util.HashSet;

public class work1 {
    public int lengthOfLongestSubstring1(String s) {
        HashSet<Character> set = new HashSet<>();
        int ret = 0;
        int left = 0;
        int right = 0;
        while (right < s.length()) {
            if (!set.contains(s.charAt(right))) {
                set.add(s.charAt(right));
            } else {
                while (left <= right && s.charAt(left) != s.charAt(right)) {
                    set.remove(s.charAt(left));
                    left++;
                }
                set.remove(left);
                left++;
            }
            set.add(s.charAt(right));
            ret = Math.max(ret, right - left + 1);
            right++;
        }
        return ret;
    }
    public int lengthOfLongestSubstring2(String s) {
        int []hash=new int[128];
        char[] arr=s.toCharArray();
        int left=0;
        int right=0;
        int ret=0;
        while (right<s.length()){
            hash[arr[right]]++;
            while (hash[arr[right]]>1){
                hash[arr[left]]--;
                left++;
            }
            ret=Math.max(ret,right-left+1);
            right++;
        }
        return ret;
    }
}
